import Route from './Route'
import type { FrameName } from './route.constant'
import type { RouteCallbackData } from './types'

export default class Router {
  public frames!: Record<FrameName, any>
  public routes!: Route[]

  constructor() {
    this.frames = {} as any
    this.routes = []
  }
  addFrame(frame: FrameName, frameInstance: any) {
    this.frames[frame] = frameInstance
  }

  addRoute(route: Route[]) {
    this.routes = this.routes.concat(route)
  }

  trigger(data: RouteCallbackData) {
    const route = this.routes.find((route) => route.name === data.name)
    if (route) {
      route.callback?.(this.frames, data)
    }
  }
}
